package com.sec.foodit;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;

public class IntroActivity extends Activity {

	private ActivityManager am = ActivityManager.getInstance();
	Timer timer = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);
		
		am.addActivity(this);
		getActionBar().hide();
		DataManager.getInstance().loadFile(this);
		
		timer = new Timer();
		TimerTask ts = new TimerTask() {
			
			@Override
			public void run() {
				timer = null;
				startActivity(new Intent("com.sec.foodit.MapActivity").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				IntroActivity.this.finish();
			}
		};
		//timer.schedule(ts, 3000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_intro, menu);
		return true;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		Log.e("foodit","x:" + event.getX() + ", y:" + event.getY());
		
		Rect rect = new Rect(260, 850, 470, 920);
		if(event.getAction()==MotionEvent.ACTION_DOWN && rect.contains((int)event.getX(), (int)event.getY()))
		{
			startActivity(new Intent("com.sec.foodit.MapActivity").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			IntroActivity.this.finish();
		}
			
		
		return super.onTouchEvent(event);
	}
	public void onLogin(View v)
	{
		//startActivity(new Intent("com.sec.foodit.HomeScreenActivity").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		startActivity(new Intent("com.sec.foodit.MapActivity").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//		ImageView iv = (ImageView)findViewById(R.id.logo);
//		BitmapDrawable bd = (BitmapDrawable)iv.getDrawable();
//		if(bd != null)
//		{
//			Bitmap bmp = bd.getBitmap();
//			bmp.recycle();
//			bmp = null;
//		}
//		bd = null;
		finish();
		//iv.setImageBitmap(null);
	}

	@Override
	protected void onDestroy() {
		
		Drawable d = findViewById(R.id.introlayout).getBackground();
		if(d instanceof BitmapDrawable)
		{
			Bitmap b = ((BitmapDrawable) d).getBitmap();
			b.recycle();
			b = null;
		}
		
		super.onDestroy();
		am.removeActivity(this);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			AlertDialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Foodit을 종료하시겠습니까?");
			//builder.setMessage("Foodit을 종료하시겠습니까?");
			builder.setPositiveButton("종료", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					am.finishAllActivity();
				}
			});
			builder.setNegativeButton("취소", null);
			builder.show();
			
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	

}
